package com.briup.impl.client;

import com.briup.entity.Environment;
import com.briup.inter.client.Client;
import com.briup.inter.support.PropertiesAware;
import org.apache.log4j.Logger;

import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Collection;
import java.util.Properties;


public class ClientImpl implements Client, PropertiesAware {
    private String host;
    private int port;
    Logger log = Logger.getLogger(ClientImpl.class);
    @Override
    public void send(Collection<Environment> list) throws Exception {
        log.debug("准备发送数据: " + list.size());
        // 客户端 -> 服务端
        try (Socket ss = new Socket(host,port);
             OutputStream os = ss.getOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(os);
        ) {
            // 1.发送数据 SocketOutputStream
            oos.writeObject(list);
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void init(Properties properties) throws Exception {
        host = properties.getProperty("client-host");
        port = Integer.parseInt(properties.getProperty("client-port"));
    }
}
